package com.hzhc.common;

import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @ClassName: FileExcelUtil
 * @Description:
 * @version 1.0
 * @author: duanhm
 * @Date: 2020/08/31 19:11:29
 * 
 */
public class FileExcelUtil {
	public static void downloadExcelIo(Workbook workbook, HttpServletRequest request, HttpServletResponse response,
			String downFileName) {
		ServletOutputStream fos = null;
		try {
			// response处理
			response.setContentType("application/octet-stream");
			String userAgent = request.getHeader("User-Agent").toLowerCase();
			if (userAgent.indexOf("msie") != -1) {
				response.addHeader("Content-Disposition",
						"attachment;filename=" + URLEncoder.encode(downFileName + ".xlsx", "UTF8"));
			} else {
				response.addHeader("Content-Disposition",
						"attachment;filename=" + new String((downFileName + ".xlsx").getBytes("UTF-8"), "ISO-8859-1"));
			}
			fos = response.getOutputStream();
			workbook.write(fos);
			fos.flush();
			fos.close();
			response.flushBuffer();
		} catch (Exception e) {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			}
		}
	}

}
